package com.anddict.action;

import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.anddict.AbsAction;
import com.anddict.AndDict;
import com.anddict.R;
import com.anddict.sql.DbAdapter;
import com.anddict.util.Reference;

public class TabFind  extends AbsAction implements OnClickListener,TextWatcher{
	
	private Button btFind;
	private Button btClear;
	private CheckBox checkCompletion;
	private AutoCompleteTextView entryTerm;
	private TextView textViewTranslate;
	private ArrayAdapter adapter;
	
	private String[] terms = new String[]{};
	
	public TabFind(AndDict activity, DbAdapter dbAdapter){
		
		super(activity, dbAdapter);
		
		Log.d(Reference.TAG, "TabFind.Constructor");
		
		btFind = (Button)activity.findViewById(R.id.btFind);
		btClear = (Button)activity.findViewById(R.id.btClear);
		btFind.setOnClickListener(this);
		btClear.setOnClickListener(this);

		checkCompletion = (CheckBox)activity.findViewById(R.id.checkCompletion);
		
		entryTerm = (AutoCompleteTextView)activity.findViewById(R.id.entryTerm);
		entryTerm.addTextChangedListener(this);
		
		textViewTranslate = (TextView)activity.findViewById(R.id.textViewTranslate);
	}
	
	public void onClick(View v) {
		
		Log.d(Reference.TAG, "TabFind.onClick - " + v.toString() );
		
        // Perform action on click
		if (v.equals(btFind)){
			find();
		}else if(v.equals(btClear)){
			clear();
		}
	}
	
	public void find(){
		
		Log.d(Reference.TAG, "TabFind.find");
		
		if (this.entryTerm.getText() == null || this.entryTerm.getText().toString().equals("") == true){
			this.getActivity().showDialog(Reference.ALERT_NO_ENTRY);
			return;
		}
		
		String translate = this.getDbAdapter().getTranslate(this.entryTerm.getText().toString());
		this.textViewTranslate.setText(translate);
	}
	
	public void clear(){
		this.entryTerm.setText("");
	}
	
	public void afterTextChanged(Editable s){
		
		Log.d(Reference.TAG, "TabFind.onClick.afterTextChanged");
		Log.d(Reference.TAG, "check " + this.checkCompletion.isChecked());
		
		if (this.checkCompletion.isChecked() == false || this.entryTerm.getText() == null || this.entryTerm.getText().toString().equals("")){
			return;
		}
		
		//adapter.clear();
		
		terms = this.getDbAdapter().searchLikeTerms(this.entryTerm.getText().toString() , 5);
		// clean terms if null or null pointer
		if (terms == null){
System.out.println("term is null");			
			terms = new String[]{};
		}
System.out.println(terms);
		adapter = new ArrayAdapter(this.getActivity(), android.R.layout.simple_dropdown_item_1line, terms);
		entryTerm.setAdapter(adapter);

	}
	
	public void beforeTextChanged(CharSequence s, int start, int count, int after){}
	public void onTextChanged(CharSequence s, int start, int before, int count){}
}
